﻿using BrnMall.Manager.Domain.AddressBook;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BrnMall.Manager.Manager.AddressBook
{
    /// <summary>
    /// 成员标签管理
    /// </summary>
    public class StaffTagManager
    {
        static string TableName = "crm_staffTag";

        public static bool Add(StaffTag model)
        {
            using (var db = new QDMallDatabase())
            {
                db.Insert(model);
            }
            return model.Id > 0;
        }

        public static bool Delete(int id)
        {
            int count = 0;
            using (var db = new QDMallDatabase())
            {
                count = db.Execute(string.Format("delete {0} where id={1}", TableName, id));
            }
            return count > 0;
        }

        public static bool Exists(int staffId, string tagName)
        {
            object obj = null;
            using (var db = new QDMallDatabase())
            {
                obj = db.ExecuteScalar<object>($"select top 1 id from {TableName} where staffId=@0 and tagName=@1", staffId, tagName);
            }
            if (obj != null && obj != DBNull.Value)
                return true;
            return false;
        }
        
        /// <summary>
        /// 标签点赞
        /// </summary>
        /// <param name="staffId"></param>
        /// <param name="tagName"></param>
        /// <returns></returns>
        public static bool UpdateUpCount(int id)
        {
            int count = 0;
            using (var db = new QDMallDatabase())
            {
                count = db.Execute($"update {TableName} set upCount=isnull(upCount,0)+1 where id={id}");
            }
            return count > 0;
        }

        public static List<StaffTag> GetList(int staffId)
        {
            using (var db = new QDMallDatabase())
            {
                return db.Fetch<StaffTag>(string.Format("select * from {0} where staffId={1}", TableName, staffId));
            }
        }
    }
}
